<template>
  <div class="oil-price-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">
        <el-icon><TrendCharts /></el-icon>
        今日油价查询
      </h1>
      <p class="page-subtitle">实时查询全国各省市最新油价信息</p>
    </div>

    <!-- 操作区域 -->
    <el-card class="filter-card">
      <div class="filter-content">
        <div class="filter-item">
          <span class="filter-label">选择省份：</span>
          <el-select
            v-model="selectedProvince"
            placeholder="选择省份查询"
            clearable
            style="width: 200px"
            @change="handleProvinceChange"
          >
            <el-option
              label="全部省份"
              value=""
            />
            <el-option
              v-for="province in provinces"
              :key="province"
              :label="province"
              :value="province"
            />
          </el-select>
        </div>
        
        <div class="filter-actions">
          <el-button
            type="primary"
            :loading="loading"
            @click="refreshData"
          >
            <el-icon><Refresh /></el-icon>
            刷新数据
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 数据展示区域 -->
    <el-card class="data-card">
      <template #header>
        <div class="card-header">
          <span>
            <el-icon><DataAnalysis /></el-icon>
            油价数据
          </span>
          <div style="display: flex; gap: 10px; align-items: center;">
            <el-button type="primary" :loading="loading" @click="handleProvinceChange">
              <el-icon><Search /></el-icon>
              查询数据
            </el-button>
            <el-button :loading="loading" @click="refreshData">
              <el-icon><Refresh /></el-icon>
              刷新数据
            </el-button>
            <el-tag v-if="lastUpdated" type="info" size="small">
              最后更新: {{ formatDate(lastUpdated) }}
            </el-tag>
          </div>
        </div>
      </template>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="5" animated />
      </div>

      <!-- 空状态 -->
      <div v-else-if="prices.length === 0" class="empty-container">
        <el-empty description="暂无油价数据" />
      </div>

      <!-- 数据表格 -->
        <div v-else>
          <el-table
            :data="tableData"
            style="width: 100%"
            :default-sort="{ prop: 'price', order: 'descending' }"
            stripe
            v-if="tableData.length > 0"
          >
            <el-table-column prop="province" label="省份" width="120" />
            <el-table-column prop="oilType" label="油品类型" width="120" />
            <el-table-column prop="price" label="价格(元/升)" width="120" sortable>
              <template #default="{ row }">
                <span class="price-text">{{ row.price.toFixed(2) }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="change" label="涨跌(元)" width="120" sortable>
              <template #default="{ row }">
                <span :class="getChangeClass(row.change)">
                  {{ row.change > 0 ? '+' : '' }}{{ row.change.toFixed(2) }}
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="changePercent" label="涨跌幅度(%)" width="150" sortable>
              <template #default="{ row }">
                <span :class="getChangeClass(row.changePercent)">
                  {{ row.changePercent > 0 ? '+' : '' }}{{ row.changePercent.toFixed(2) }}%
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="lastUpdated" label="更新时间" width="180">
              <template #default="{ row }">
                {{ formatDate(row.lastUpdated) }}
              </template>
            </el-table-column>
          </el-table>
          
          <div v-else class="empty-container">
            <el-empty description="请选择省份并点击查询按钮获取油价数据">
              <el-button type="primary" @click="handleProvinceChange">
                <el-icon><Search /></el-icon>
                立即查询
              </el-button>
            </el-empty>
          </div>
        </div>
    </el-card>

    <!-- 统计卡片 -->
    <el-row v-if="prices.length > 0 && tableData.length > 0" :gutter="20" class="stats-row">
      <el-col :xs="24" :sm="12" :md="6" v-for="oilType in uniqueOilTypes" :key="oilType">
        <el-card class="stat-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-title">{{ oilType }}</div>
            <div class="stat-price">
              {{ getAveragePrice(oilType).toFixed(2) }}元/升
            </div>
            <div class="stat-desc">
              {{ selectedProvince || '全国' }}均价
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue';
import { useOilPriceStore } from '../stores/oilPrice';
import { Search, Refresh } from '@element-plus/icons-vue';

const oilPriceStore = useOilPriceStore();

// 计算属性
const prices = computed(() => oilPriceStore.prices);
const loading = computed(() => oilPriceStore.loading);
const provinces = computed(() => oilPriceStore.provinces);
const selectedProvince = computed({
  get: () => oilPriceStore.selectedProvince,
  set: (value: string) => oilPriceStore.setSelectedProvince(value)
});
const lastUpdated = computed(() => oilPriceStore.lastUpdated);

// 表格数据
const tableData = computed(() => oilPriceStore.currentProvincePrices);

// 唯一的油品类型
const uniqueOilTypes = computed(() => {
  const types = new Set(prices.value.map(p => p.oilType));
  return Array.from(types);
});

// 获取平均价格
const getAveragePrice = (oilType: string) => {
  const typePrices = prices.value.filter(p => p.oilType === oilType);
  if (typePrices.length === 0) return 0;
  return typePrices.reduce((sum, p) => sum + p.price, 0) / typePrices.length;
};

// 获取涨跌样式类
const getChangeClass = (change: number) => {
  if (change > 0) return 'change-positive';
  if (change < 0) return 'change-negative';
  return 'change-neutral';
};

// 格式化日期
const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleString('zh-CN');
};

// 事件处理
const handleProvinceChange = () => {
  oilPriceStore.fetchOilPrices();
};

const refreshData = () => {
  oilPriceStore.refreshData();
};

// 初始化
onMounted(() => {
  oilPriceStore.initialize();
});
</script>

<style scoped>
.oil-price-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-title {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.page-subtitle {
  font-size: 16px;
  color: #909399;
}

.filter-card {
  margin-bottom: 20px;
}

.filter-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 15px;
}

.filter-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.filter-label {
  font-weight: 500;
  color: #606266;
}

.data-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.loading-container {
  padding: 40px;
}

.empty-container {
  padding: 40px;
}

.price-text {
  font-weight: bold;
  color: #409eff;
}

.change-positive {
  color: #f56c6c;
  font-weight: bold;
}

.change-negative {
  color: #67c23a;
  font-weight: bold;
}

.change-neutral {
  color: #909399;
}

.stats-row {
  margin-top: 20px;
}

.stat-card {
  text-align: center;
}

.stat-content {
  padding: 10px 0;
}

.stat-title {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.stat-price {
  font-size: 24px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 5px;
}

.stat-desc {
  font-size: 12px;
  color: #c0c4cc;
}

@media (max-width: 768px) {
  .filter-content {
    flex-direction: column;
    align-items: stretch;
  }
  
  .filter-item {
    justify-content: space-between;
  }
  
  .stats-row .el-col {
    margin-bottom: 15px;
  }
}
</style>